package com.copa.l855;

import java.util.Set;
import java.util.TreeSet;

/**
 * @author copa
 * @createDate 2022-12-30 10:31
 * @function 常规法
 */
public class Test1 {

    private Integer student;
    private Set<Integer> students;

    public Test1(int n) {
        this.student = n;
        this.students = new TreeSet<>();
    }

    public int seat() {
        // 默认0，如果集合长度为0直接0
        int result = 0;
        if (students.size() > 0) {
            int left = -1;
            int distinct = students.stream().findFirst().get();
            for (Integer num : students) {
                if (left != -1) {
                    // 中间间隔距离判断
                    if (distinct < (num - left) / 2) {
                        result = (num + left) / 2;
                        distinct = (num - left) / 2;
                    }
                }
                left = num;
            }
            if (student - 1 - left > distinct) {
                // 判断最大座位数与当前基准数的距离
                result = student - 1;
            }
        }
        students.add(result);
        return result;
    }

    public void leave(int p) {
        students.remove(p);
    }

    public static void main(String[] args) {
        int n = 10;
        Test1 instance = new Test1(n);
        instance.seat();
        instance.seat();
        instance.seat();
        instance.seat();
        instance.leave(4);
        instance.seat();
        for (Integer student : instance.students) {
            System.out.println(student);
        }
    }
}
